package com.zslibrary.Protocol;

import android.util.SparseArray;

import com.zslibrary.Enum.PipeResultDefine;
import com.zslibrary.Enum.Protocol;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Enum.StatusDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Structure.ComponentStruct;
import com.zslibrary.Structure.GeneralRequestStruct;
import com.zslibrary.Structure.MessageStruct;
import com.zslibrary.Tools.FrameBuilder;
import com.zslibrary.Tools.ZSLog;

public class ComponentOption{
    private static Boolean isStart=false;

    public static int initComponent(final Pipe pipe){
        final GeneralRequestStruct request=new GeneralRequestStruct();
        SparseArray<ComponentStruct> componentInfos= Machine.getComponentList();
        int data_len=2+componentInfos.size()*(2+4);
        FrameBuilder dataBuilder=new FrameBuilder(data_len);

        if(componentInfos.size()==0)
            return StateEventDefine.EVT_NONE;
        dataBuilder.append((short)componentInfos.size());
        for(int i=0;i<componentInfos.size();i++){
            dataBuilder.append((short)componentInfos.keyAt(i));
            dataBuilder.append(3);
        }
        request.requstId= Protocol.MSG_SET_COMPONENT_OPTION_INFOS;
        request.dataLength=(short)data_len;
        request.data=dataBuilder.toBytes();
        Pipe.HandleMessageCallback callback=new Pipe.HandleMessageCallback() {
            @Override
            public Boolean handle(MessageStruct[] messages) {
                for(MessageStruct message:messages){
                    switch(message.messageId){
                        case Protocol.MSG_SET_COMP_ERR:{
                            CommonMessage.processSetError(message);
                        }break;

                        case Protocol.MSG_CLEAR_COMP_ERR:{
                            CommonMessage.processClrError(message);
                        }break;

                        case Protocol.MSG_BOARD_STATUS:{
                            int status=CommonMessage.processBoardStatus(message);
                            if(StatusDefine.CMS_DIAGNOSIS==status){
                                isStart=true;   /*一直不return true，pipe会报超时*/
                            }
                            if(isStart&&(StatusDefine.CMS_IDLE==status))
                                return true;
                        }break;

                        case Protocol.MSG_BOARD_RESET:{
                            pipe.interruptAsynchonousPipe();    /*可被终止*/
                        }break;

                        case Protocol.MSG_REPORT_STR: {
                            String reportStr = new String(message.messageData);
                            //ZSLog.d("report string:" + reportStr);
                        }break;


                        default:
                            break;
                    }
                }
                return false;
            }
        };
        isStart=false;
        int pipeResult=pipe.asynchronousPipe(request,callback,60000);
        if(pipeResult==PipeResultDefine.ASYNC_BUSY)
            return StateEventDefine.EVT_EXECUTE;
        if(pipeResult==PipeResultDefine.ASYNC_ERROR)
            return StateEventDefine.EVT_DISCONNECT;
        if(pipeResult==PipeResultDefine.ASYNC_INTERRUPT)
            return StateEventDefine.EVT_RESET;
        return StateEventDefine.EVT_NONE;
    }

}
